//
//  ChangePassWordViewController.m
//  tabbar
//
//  Created by KWSD_F on 16/5/27.
//  Copyright © 2016年 孙添凤. All rights reserved.
//
#import "NetRequest.h"
#import "ChangePassWordViewController.h"
//#import "userModel.h"
#import "Masonry.h"
#import "UIColor+CL.h"
//#import "attendance_url.h"
/*_oldPassWord.tag = 101;
 _passWordNew.tag = 102;
 _againPassWord.tag =103;*/

#define oldPassWordtag 31415926
#define passWordNewtag 31415927
#define againPassWordtag 31415928

@interface ChangePassWordViewController ()<UITextFieldDelegate>
{
    NSTimer *myTimer;
}
@property (nonatomic, copy)UITextField *oldPassWord;
@property (nonatomic, copy)UITextField *passWordNew;
@property (nonatomic, copy)UITextField *againPassWord;
@property (nonatomic, copy)UILabel *lable1;
@property (nonatomic, copy)UILabel *lable2;
@property (nonatomic, copy)UILabel *lable3;
@property (nonatomic, strong) UIAlertView *alert;
@property (nonatomic, copy)UIView *backgroundView;
@property (nonatomic, copy)UIButton *btn;
@property (nonatomic, assign) NSString *userName;
@property (strong, nonatomic)  UIView * alphaView;


@property (strong, nonatomic)NSString *warningStr;
@property (strong, nonatomic)NSString *PromptStr;
@property (strong, nonatomic)NSString *messageStr1;//原密码不正确
@property (strong, nonatomic)NSString *messageStr2;//两次输入的密码不一致
@property (strong, nonatomic)NSString *messageStr3;//密码不能为空
@property (strong, nonatomic)NSString *messageStr4;//密码格式不正确
@property (strong, nonatomic)NSString *messageStr5;//密码修改成功，请牢记密码
@property (strong, nonatomic)NSString *messageStr6;//新密码和旧密码相同，请重新输入
@property (strong, nonatomic)NSString *messageStr7;//网络错误，正在紧急抢修ing~
@property (strong, nonatomic)NSString *SureStr;//确定
@property (strong, nonatomic)NSString *GoodStr;//好


@end

@implementation ChangePassWordViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.cusTabView.hidden = YES;
}



- (void)viewDidLoad
{
    
    [super viewDidLoad];

//    NSString *warningStr;
//    NSString *PromptStr;
//    NSString *messageStr1;//原密码不正确
//    NSString *messageStr2;//两次输入的密码不一致
//    NSString *messageStr3;//密码不能为空
//    NSString *messageStr4;//密码格式不正确
//    NSString *messageStr5;//密码修改成功，请牢记密码
//    NSString *messageStr6;//新密码和旧密码相同，请重新输入
//    NSString *messageStr7;//网络错误，正在紧急抢修ing~
//    NSString *SureStr;//确定
//    NSString *GoodStr;//好
    //这里添加功能判断
    if (getEnglishLanguage()) {
        _warningStr  = @"Warning";
        _PromptStr   = @"Prompt";
        _messageStr1 = @"The old password is not correct";
        _messageStr2 = @"Two input passwords inconsistent";
        _messageStr3 = @"Password can not be empty";
        _messageStr4 = @"The password you typed is incorrect.";
        _messageStr5 = @"Password successfully changed, please keep in mind the password";
        _messageStr6 = @"The new password is the same as the old one, please re-enter";
        _messageStr7 = @"Network error, Emergency repairing";
        _SureStr     = @"OK";
        _GoodStr     = @"OK";
    }else{
        _warningStr = @"警告";
        _PromptStr = @"提示";
        _messageStr1 = @"原密码不正确";
        _messageStr2 = @"两次输入的密码不一致";
        _messageStr3 = @"密码不能为空";
        _messageStr4 = @"密码格式不正确";
        _messageStr5 = @"密码修改成功，请牢记密码";
        _messageStr6 = @"新密码和旧密码相同，请重新输入";
        _messageStr7 = @"网络错误，正在紧急抢修ing~";
        _SureStr = @"确定";
        _GoodStr = @"好";
    }

    
    
    
    
    _alphaView = [[UIView alloc] init];
    _alphaView.alpha = 0.5;
    _alphaView.backgroundColor = [UIColor whiteColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(infoAction) name:UITextFieldTextDidChangeNotification object:nil];
    
    if ([getUIStyle() isEqualToString:@"0"]) {
        //这里添加功能判断
        if (getEnglishLanguage()) {
            [self setTitleLabelText:@"Change Password"];
        }else{
            [self setTitleLabelText:@"修改密码"];
        }

        
    }else{
        //这里添加功能判断
        if (getEnglishLanguage()) {
             [self setTitleLabelText:@"Password"];
        }else{
             [self setTitleLabelText:@"修改密码"];
        }
       
    }
    
    self.view.backgroundColor = [UIColor colorWithRed:244.0/255.0 green:243.0/255.0 blue:242.0/255.0 alpha:1];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    [self createUI];
    [self createLeftNavBar];
    _againPassWord.delegate = self;
    _passWordNew.delegate = self;
    _oldPassWord.delegate = self;
    _oldPassWord.tag = oldPassWordtag;
    _passWordNew.tag = passWordNewtag;
    _againPassWord.tag = againPassWordtag;
}

- (void)creatUIFormasonry
{
    
    //    UIButton *scBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    //    scBtn.frame = CGRectMake(0, 0, 30, 30);
    //
    //    [scBtn setTitle:@"返回" forState:UIControlStateNormal];
    //
    //    //    [btn setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    //    [scBtn addTarget:self action:@selector(itemClick) forControlEvents:UIControlEventTouchUpInside];
    //    //    [btn setBackgroundImage:[UIImage imageNamed:@"haoyou"] forState:UIControlStateNormal];
    //
    //    UIBarButtonItem *dItem = [[UIBarButtonItem alloc]initWithCustomView:scBtn];
    //    //    self.navigationItem.rightBarButtonItem = dItem;
    //    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects: dItem,nil];
    //
    //
    //    _backgroundView = [[UIView alloc]init];
    //    [_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(self.view.mas_left);
    //        make.top.equalTo(@(49));
    //        make.right.equalTo(self.view.mas_right);
    //        make.height.equalTo(@(150));
    //    }];
    //    _backgroundView.backgroundColor = [UIColor redColor];
    //    [self.view addSubview:_backgroundView];
    //
    //
    //    _oldPassWord = [UITextField alloc];
    //    _passWordNew = [UITextField alloc];
    //    _againPassWord = [UITextField alloc];
    //    _lable1 = [[UILabel alloc] init];
    //    _lable2 = [[UILabel alloc] init];
    //    _lable3 = [[UILabel alloc] init];
    //    _lable1.backgroundColor = [UIColor whiteColor];
    //    _lable2.backgroundColor = [UIColor whiteColor];
    //    _lable3.backgroundColor = [UIColor whiteColor];
    //    UILabel *passwordLb = [[UILabel alloc] init];
    //    passwordLb.text = @"原密码";
    //    UILabel *newPWLb = [[UILabel alloc] init];
    //    newPWLb.text = @"新密码";
    //    UILabel *passWordAGLb = [[UILabel alloc] init];
    //    passWordAGLb.text = @"请在输入一次";
    //
    //
    //    [_lable1 mas_makeConstraints:^(MASConstraintMaker *make) {
    //
    //    }];
    //    [_lable2 mas_makeConstraints:^(MASConstraintMaker *make) {
    //
    //    }];
    //    [_lable3 mas_makeConstraints:^(MASConstraintMaker *make) {
    //
    //    }];
    
}

- (void)createUI
{
    UIView *backview1 = [[UIView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 50)];
    UIView *backview2 = [[UIView alloc] initWithFrame:CGRectMake(0, 71, self.view.frame.size.width, 50)];
    UIView *backview3 = [[UIView alloc] initWithFrame:CGRectMake(0, 122, self.view.frame.size.width, 50)];
    backview1.backgroundColor = [UIColor whiteColor];
    backview2.backgroundColor = [UIColor whiteColor];
    backview3.backgroundColor = [UIColor whiteColor];
    
    //CGRect frame1 = backview1.frame;
    //CGRect frame2 = backview2.frame;
    //CGRect frame3 = backview3.frame;
    
    UILabel *lb1 = [[UILabel alloc] init];
    UILabel *lb2 = [[UILabel alloc] init];
    UILabel *lb3 = [[UILabel alloc] init];
    
    if ([getUIStyle() isEqualToString:@"0"]) {
        
    }else{
        
    }
    
    
    
    _oldPassWord = [[UITextField alloc] init];
    _passWordNew = [[UITextField alloc] init];
    _againPassWord = [[UITextField alloc] init];
    
    NSString *lbText1;
    NSString *lbText2;
    NSString *lbText3;
    NSString *OldPasswordStr;//Please enter the original password
    NSString *NewPasswordStr;
    NSString *RetypePasswordStr;
    //这里添加功能判断
    if (getEnglishLanguage()) {
        OldPasswordStr = @"Please enter the original password";
        NewPasswordStr = @"Please enter a new password";
        RetypePasswordStr = @"Please enter a new password again";
        lbText1 = @"Old Password";
        lbText2 = @"New Password";
        lbText3 = @"Retype Password";
    }else{
        OldPasswordStr = @"请输入原密码";
        NewPasswordStr = @"请输入新密码";
        RetypePasswordStr = @"请再输入一次新密码";
        lbText1 = @"旧密码";
        lbText2 = @"新密码";
        lbText3 = @"确认密码";
    }

    
    
    
//    if ([getUIStyle() isEqualToString:@"0"]) {
//        lb1.text = @"Old Password";
//        lb2.text = @"New Password";
//        lb3.text = @"Retype Password";
//        _oldPassWord.placeholder = @"请输入原密码";
//        _passWordNew.placeholder = @"请输入新密码";
//        _againPassWord.placeholder = @"请再输入一次新密码";
//    }else{
        lb1.text = lbText1;
        lb2.text = lbText2;
        lb3.text = lbText3;
        _oldPassWord.placeholder = OldPasswordStr;
        _passWordNew.placeholder = NewPasswordStr;
        _againPassWord.placeholder = RetypePasswordStr;
//    }
    
    
    
    
    
    
    _passWordNew.clearButtonMode = UITextFieldViewModeWhileEditing;
    _oldPassWord.clearButtonMode = UITextFieldViewModeWhileEditing;
    _againPassWord.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    
    
    _oldPassWord.returnKeyType = UIReturnKeyNext;
    _passWordNew.returnKeyType = UIReturnKeyNext;
    _againPassWord.returnKeyType = UIReturnKeyDone;
    
    _oldPassWord.secureTextEntry = YES;
    _passWordNew.secureTextEntry = YES;
    _againPassWord.secureTextEntry = YES;
    
    /*imageView.layer.masksToBounds = YES;
     imageView.layer.cornerRadius = 50.0;
     imageView.layer.borderColor = [UIColor whiteColor].CGColor;
     imageView.layer.borderWidth = 3.0f;
     imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;
     imageView.layer.shouldRasterize = YES;
     imageView.clipsToBounds = YESS; */
    
    
    
    
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    if ([getUIStyle() isEqualToString:@"0"]) {
        
    }else{
        
    }
    //这里添加功能判断
    if (getEnglishLanguage()) {
        [_btn setTitle:@"modify" forState:UIControlStateNormal];
    }else{
        [_btn setTitle:@"确认修改" forState:UIControlStateNormal];
    }

    
    //    _btn.layer.cornerRadius = 10.0f;
//    _btn.frame = CGRectMake(backview3.frame.origin.x+10, backview3.frame.origin.y+80, backview3.frame.size.width-20, 50);
    _btn.titleLabel.font = [UIFont systemFontOfSize:20];
    [_btn setBackgroundColor:[UIColor colorWithHex:0xd7000f]];
    [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    
    //203.66.63
    [_btn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHex:0xd7000f]] forState:UIControlStateNormal];
    [_btn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHex:0xdf333f]] forState:UIControlStateHighlighted];
    [_btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    [backview1 addSubview:_oldPassWord];
    [backview2 addSubview:_passWordNew];
    [backview3 addSubview:_againPassWord];
    
    [self.view addSubview:backview1];
    [self.view addSubview:backview2];
    [self.view addSubview:backview3];
    [self.view addSubview:lb1];
    [self.view addSubview:lb2];
    [self.view addSubview:lb3];
    [self.view addSubview:_btn];
    
    [lb1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(32/2);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    [backview1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb1.mas_bottom).offset(32/2);
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.equalTo(@(45));
        make.left.equalTo(self.view.mas_left).offset(30);
    }];
    [lb2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backview1.mas_bottom).offset(32/2);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    [backview2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb2.mas_bottom).offset(32/2);
        make.centerX.equalTo(self.view.mas_centerX);
        make.left.equalTo(self.view.mas_left).offset(30);
        make.height.equalTo(@(45));
    }];
    [lb3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backview2.mas_bottom).offset(32/2);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    [backview3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb3.mas_bottom).offset(32/2);
        make.centerX.equalTo(self.view.mas_centerX);
        make.left.equalTo(self.view.mas_left).offset(30);
        make.height.equalTo(@(45));
    }];
    [_oldPassWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(backview1);
    }];
    
    _oldPassWord.textAlignment = NSTextAlignmentCenter;
    _passWordNew.textAlignment = NSTextAlignmentCenter;
    _againPassWord.textAlignment = NSTextAlignmentCenter;
    
//#pragma mark - 改变placeholder的字体大小
//    [_oldPassWord setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
//#pragma mark - 改变placeholder的文字颜色
//    [_oldPassWord setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
    
//    [_oldPassWord setValue:@(NSTextAlignmentLeft) forKey:@"_placeholderLabel.textAlignment"];
    
    
    [_passWordNew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(backview2);
    }];
    [_againPassWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(backview3);
    }];
    [_btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
        make.height.equalTo(@(50));
    }];
//    backview2.backgroundColor = [UIColor brownColor];
    
    backview2.layer.masksToBounds = YES;
    backview2.layer.cornerRadius = 5.0;
    backview2.layer.borderColor = [UIColor colorWithHex:0xababab].CGColor;
    backview2.layer.borderWidth = 1.0f;
    backview2.layer.rasterizationScale = [UIScreen mainScreen].scale;
    backview2.layer.shouldRasterize = YES;
    backview2.clipsToBounds = YES;
    
    backview1.layer.masksToBounds = YES;
    backview1.layer.cornerRadius = 5.0;
    backview1.layer.borderColor = [UIColor colorWithHex:0xababab].CGColor;
    backview1.layer.borderWidth = 1.0f;
    backview1.layer.rasterizationScale = [UIScreen mainScreen].scale;
    backview1.layer.shouldRasterize = YES;
    backview1.clipsToBounds = YES;
    
    backview3.layer.masksToBounds = YES;
    backview3.layer.cornerRadius = 5.0;
    backview3.layer.borderColor = [UIColor colorWithHex:0xababab].CGColor;
    backview3.layer.borderWidth = 1.0f;
    backview3.layer.rasterizationScale = [UIScreen mainScreen].scale;
    backview3.layer.shouldRasterize = YES;
    backview3.clipsToBounds = YES;
    
    /*mageView.layer.masksToBounds = YES;
     imageView.layer.cornerRadius = 50.0;
     imageView.layer.borderColor = [UIColor whiteColor].CGColor;
     imageView.layer.borderWidth = 3.0f;
     imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;
     imageView.layer.shouldRasterize = YES;
     imageView.clipsToBounds = YESS;
     */
    
    [_btn addSubview:_alphaView];
    [_alphaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_btn);
    }];
    _alphaView.hidden = NO;
    
    
    
}


#pragma mark - 监听键盘的情况
- (void)infoAction

{
    
    NSLog(@"------?%@?---------",_passWordNew.text);
    if (_oldPassWord.text.length != 0&&_passWordNew.text.length != 0 &&_againPassWord.text.length != 0) {
        _alphaView.hidden = YES;
//        NSLog(@"拉克丝大姐夫拉克京东方");
        
    }else{
        _alphaView.hidden = NO;
        NSLog(@"");
    }
    
    
    
    //    NSIndexPath *index = [NSIndexPathindexPathForRow:0inSection:1];
    //
    //    OrderConfirmInfoTableViewCell *cell = (OrderConfirmInfoTableViewCell *)[_tableViewcellForRowAtIndexPath:index];
    //
    //    _phoneStr = cell.phoneField.text;
    //
    //    _addrStr = cell.addressField.text;
    
}




- (void)drawPlaceholderInRect:(CGRect)rect {
    [_oldPassWord drawPlaceholderInRect:CGRectMake(_oldPassWord.frame.size.width/2, 0, 0, 0)];
    [_passWordNew drawPlaceholderInRect:CGRectMake(_oldPassWord.frame.size.width/2, 0, 0, 0)];
    [_againPassWord drawPlaceholderInRect:CGRectMake(_oldPassWord.frame.size.width/2, 0, 0, 0)];
}



#pragma -mark 确认按钮点击事件
- (void)btnClick
{
    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
//    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
//    NSString *str = [ud objectForKey:@"fielterMainId"];
    
    NSString *passWord = [ud objectForKey:@"pass"];
    
    [_oldPassWord resignFirstResponder];
    [_passWordNew resignFirstResponder];
    [_againPassWord resignFirstResponder];
//    NSString *name = [ud objectForKey:@"name"];
    
    float version=[[[UIDevice currentDevice] systemVersion] floatValue];
    if (version<8) {
        
        if(![_oldPassWord.text isEqualToString:passWord]){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_warningStr message:_messageStr1 delegate:self cancelButtonTitle:_SureStr otherButtonTitles: nil];
            [alert show];
            return;
        }else{
            if (![_passWordNew.text isEqualToString:_againPassWord.text]) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_PromptStr message:_messageStr2 delegate:self cancelButtonTitle:_SureStr otherButtonTitles: nil];
                [alert show];
                return;
            }else if(_passWordNew.text.length == 0){
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_PromptStr message:_messageStr3 delegate:self cancelButtonTitle:_SureStr otherButtonTitles: nil];
                [alert show];
                return;
            }else if(![self checkPassWord:_passWordNew.text]){
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_PromptStr message:_messageStr4 delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                
                [alert show];
                return;
            }else{
                NSString *str;
                NSString *choosePassWordUrl;
                if ([getUIStyle() isEqualToString:STYLE0]) {
                    choosePassWordUrl = passWordUrlMain;
                    str = userId();
                    [param setObject:str forKey:@"userId"];
                }else{
                    choosePassWordUrl = passWordUrl;
//                    str = [ud objectForKey:@"fielterMainId"];
                     str = [ud objectForKey:@"USERID"];
                    [param setObject:str forKey:@"stuId"];
                }
                
                [param setObject:@"1" forKey:@"source"];
                
                
                [param setObject:passWord forKey:@"oldPassword"];
                [param setObject:_passWordNew.text forKey:@"newPassword"];
                
                //            [self.navigationController popToRootViewControllerAnimated:YES];
                
                //phone_resetMainProjectPassword.do?userId=&oldPassword=&newPassword=
                //phone_resetStuPassword.do?stuId=&oldPassword=&newPassword=
                
                
                [param setObject: getSessionId() forKey:@"sessionId"];
                
                NSString *url = [NSString stringWithFormat:@"%@%@",self.programaUrl,choosePassWordUrl];
                [NetRequest requestUrl:url andParamas:param andReturnBlock:^(NSData *data, NSError *error) {
                    if (!error&&data!= nil) {
                        id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                        if ([obj valueForKey: @"promptMessage"]) {
                            [[NSNotificationCenter defaultCenter]postNotificationName:@"relogin" object:@"relogin"];
                            
                        }
                        
                        NSString *message = [obj objectForKey:@"message"];
                        
                        
                        if ([str isEqualToString:@"1"]) {
                            
                            NSLog(@"%@",[ud objectForKey:@"pass"]);
                            [ud removeObjectForKey:@"pass"];
                            [ud setObject:_passWordNew forKey:@"pass"];
                            NSLog(@"%@",[ud objectForKey:@"pass"]);
//                            [ud removeObjectForKey:@"userPassWord"];
//                            [ud setObject:_passWordNew.text forKey:@"userPassWord"];
                            [ud synchronize];
//                            NSLog(@"%@",[ud objectForKey:@"userPassWord"]);
                            [self rememberWrite];
                            _alert = [[UIAlertView alloc] initWithTitle:_PromptStr message:_messageStr5 delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                            [_alert show];
//                            [self.navigationController popToRootViewControllerAnimated:YES];
                        }else if([message isEqualToString:@"新密码和旧密码相同，请重新输入"]){
                            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_PromptStr message:_messageStr6 delegate:self cancelButtonTitle:_GoodStr otherButtonTitles: nil];
                            [alert show];
                        }
                        
                    }else{
                        //NSString *str = [NSString stringWithFormat:@"%@", error];
                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:_warningStr message:_messageStr7 delegate:self cancelButtonTitle:_SureStr otherButtonTitles:nil, nil];
                        [alert show];
                        return;
                        
                    }
                }];
                
            }
        }
    }else{
        NSLog(@"%@<----->%@",_oldPassWord.text,passWord);
        
        
        if(![_oldPassWord.text isEqualToString:passWord]){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_warningStr message:_messageStr1 delegate:self cancelButtonTitle:_SureStr otherButtonTitles: nil];
            [alert show];
            return;
        }else{
            if (![_passWordNew.text isEqualToString:_againPassWord.text]) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_PromptStr message:_messageStr2 delegate:self cancelButtonTitle:_SureStr otherButtonTitles: nil];
                [alert show];
                return;
            }else if(_passWordNew.text.length == 0){
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_PromptStr message:_messageStr3 delegate:self cancelButtonTitle:_SureStr otherButtonTitles: nil];
                [alert show];
                return;
            }else if(![self checkPassWord:_passWordNew.text]){
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_PromptStr message:_messageStr4 delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                
                [alert show];
                return;
            }else{
                NSString *str;
                NSString *choosePassWordUrl;
                if ([getUIStyle() isEqualToString:STYLE0]) {
                    choosePassWordUrl = passWordUrlMain;
                    str = userId();
                    [param setObject:str forKey:@"userId"];
                }else{
                    choosePassWordUrl = passWordUrl;
//                    str = [ud objectForKey:@"fielterMainId"];
                    str = [ud objectForKey:@"USERID"];
                    [param setObject:passWord forKey:@"oldPassword"];
                    [param setObject:str forKey:@"stuId"];
                }

                
                
                
                NSLog(@"%@<---->%@",_oldPassWord.text,passWord);
//                NSString *str = [ud objectForKey:@"userid"];
//                [param setObject:@"1" forKey:@"source"];
//                [param setObject:str forKey:@"stuId"];
                [param setObject:passWord forKey:@"oldPassword"];
                [param setObject:_passWordNew.text forKey:@"newPassword"];
                
                //            [self.navigationController popToRootViewControllerAnimated:YES];
                
                
                
                
                
                
                NSString *url = [NSString stringWithFormat:@"%@%@",self.programaUrl,choosePassWordUrl];
                [NetRequest requestUrl:url andParamas:param andReturnBlock:^(NSData *data, NSError *error) {
                    if (!error&&data!= nil) {
                        id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                        
                        NSString *message = [obj objectForKey:@"message"];
                        
                        
                        if ([message isEqualToString:@"密码修改成功"]) {
                            
                            
                            NSLog(@"%@",[ud objectForKey:@"pass"]);
                            [ud removeObjectForKey:@"pass"];
                            [ud setObject:_passWordNew.text forKey:@"pass"];
                            NSLog(@"%@",[ud objectForKey:@"pass"]);
//                            NSLog(@"%@",[ud objectForKey:@"userPassWord"]);
//                            [ud removeObjectForKey:@"userPassWord"];
//                            [ud setObject:_passWordNew.text forKey:@"userPassWord"];
                            [ud synchronize];
//                            NSLog(@"%@",[ud objectForKey:@"userPassWord"]);
                            [self rememberWrite];
                            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:_PromptStr message:_messageStr5 preferredStyle:UIAlertControllerStyleAlert];
                            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                                [self.navigationController popViewControllerAnimated:YES];
                                NSLog(@"这里+++++++");
                                
                            }];
                            [alertController addAction:cancelAction];
                            [self presentViewController:alertController animated:YES completion:^{
                                
                            }];
                            [self.navigationController popToRootViewControllerAnimated:YES];
                        }else if([message isEqualToString:@"新密码和旧密码相同，请重新输入"]){
                            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_PromptStr message:_messageStr6 delegate:self cancelButtonTitle:_GoodStr otherButtonTitles: nil];
                            [alert show];
                        }
                        
                        
                    }else{
                        //NSString *str = [NSString stringWithFormat:@"%@", error];
                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:_warningStr message:_messageStr7 delegate:self cancelButtonTitle:_SureStr otherButtonTitles:nil, nil];
                        [alert show];
                        return;
                        
                    }
                }];

                
            }
        }

        
        
        
        
    }
    

    
    
    //192.168.6.46:8080/sjwd_edus_auto/update_updateInfo.do?t0_au_19_1436=79&t0_au_19_1436_1979_password=000000&tableId=19&pageId=1436&fielterMainId=79
    
    
}
-(void)popAlertView{
    
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma -mark 颜色转图片
- (UIImage*) createImageWithColor: (UIColor*) color
{
    CGRect rect=CGRectMake(0,0, 1, 1);
//    CGFloat scale = [UIScreen mainScreen].scale;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    return theImage;
}

#pragma -mark 收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (BOOL)checkPassWord:(NSString *)str

{
    
    NSString *regex = @"^[0-9_a-zA-Z]{6,20}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isMatch = [pred evaluateWithObject:str];
    if ([str length] != 0) {
        if (!isMatch) {
            //            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            //            [alert show];
            return NO;
        }
    }
    return YES;
    
}




#pragma -mark 键盘回车按钮点击事件
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"tag = %zd",textField.tag);
    if (textField.tag == againPassWordtag) {
        [textField resignFirstResponder];
        [self btnClick];
    }else{
        
        //利用tag值找到下面的一个文本输入框
        UITextField *tf = (UITextField *)[self.view viewWithTag:textField.tag+1];
        //成为第一响应者(弹出键盘)
        [tf becomeFirstResponder];
    }
    
    
    return YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.oldPassWord resignFirstResponder];
    [self.passWordNew resignFirstResponder];
    [self.againPassWord resignFirstResponder];
    self.cusTabView.userInteractionEnabled = NO;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"zheli");
    [myTimer invalidate];
    if ([alertView isKindOfClass:_alert.class]) {
        NSLog(@"zhelilaksdjf");
        
        myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(popAlertView) userInfo:nil repeats:NO];
    }
}
#pragma -mark 归档
-(void)rememberWrite{
    
//    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    
    NSLog(@"$@!");
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSString *user = [ud objectForKey:@"user"];
    NSString *password = [ud objectForKey:@"pass"];
    NSLog(@"用户名：%@\n密码：%@",user,password);
//    if () {
//        <#statements#>
//    }
//    userModel *model = [userModel defaultUsearModel];
//    model.userName = name;
//    model.passWord = _passWordNew.text;
//    [userModel guidang];
//    userModel *userMo = [userModel jiedang];
//    NSLog(@"%@", userMo);
    
    
    
    
//    NSLog(@"%@", userMo);
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
